package org.log4jconfig.xml.editor.contentassist.template;

import java.util.List;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.text.templates.DocumentTemplateContext;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.log4jconfig.xml.editor.utils.JdtUtils;

@SuppressWarnings("restriction")
public class BasePackageVariableResolver extends TemplateVariableResolver {

	/**
	 * 
	 */
	@Override
	public void resolve(final TemplateVariable variable, final TemplateContext context) {
		if (context instanceof DocumentTemplateContext && variable.getName().equals("pkg")) {
			DocumentTemplateContext documentTemplateContext = (DocumentTemplateContext) context;
			if (documentTemplateContext.getDocument() instanceof IStructuredDocument) {

				IJavaProject javaProject = JdtUtils.getJavaProject((IStructuredDocument) documentTemplateContext
						.getDocument());
				List<String> packages = JdtUtils.getAllSourcePackages(javaProject);
				String[] values = new String[packages.size()];
				variable.setValues(packages.toArray(values));
			}
			
		} else {
			super.resolve(variable, context);
		}

	}

}
